/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************
 * MIFSS  - content storage system
 * 
 *
 * @uthors: uros.kristan@gmail.com (Uroš Kristan ) Urosk.NET
 *         jernej.svigelj@gmail.com (Jernej Švigelj) 
 */
package net.monare.mifss.services;

import net.monare.mifss.configurations.pojo.*;
import net.monare.mifss.exceptions.ConfigurationHandlerException;
import net.monare.mifss.exceptions.StorageManagerException;
import net.monare.mifss.exceptions.StorageServiceException;
import net.monare.mifss.lib.StorageProgressWalker;
import net.monare.mifss.managers.ContentManager;
import net.monare.mifss.managers.StorageManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StorageService {

    @Autowired
    private StorageManager storageManager;

    @Autowired
    private ContentManager contentManager;


/*
############################################################
 STORAGE SET






############################################################
 */

    public void createStorageSetDefintion(StorageSetDef storageSetDef) throws StorageServiceException {

        try {
            storageManager.createStorageSetDefinition(storageSetDef);
        } catch (ConfigurationHandlerException e) {
            throw new StorageServiceException(e);
        }

    }

    public void lockStorageSet(String storageSetName, String lockedBy, boolean locked) throws StorageServiceException {

        try {
            storageManager.setLockToStorageSetDefinition(storageSetName, locked, lockedBy);
        } catch (ConfigurationHandlerException e) {
            throw new StorageServiceException(e);
        }

    }

    public void removeStorageSetDefinition(String storageSetDefName) throws StorageServiceException {

        try {
            storageManager.removeStorageSetDefinition(storageSetDefName);
        } catch (ConfigurationHandlerException e) {
            throw new StorageServiceException(e);
        }

    }

    public List<String> listStorageSetNames() throws StorageServiceException {
        try {
            return storageManager.listStorageSetNames();
        } catch (ConfigurationHandlerException e) {
            throw new StorageServiceException(e);
        }
    }

    public List<StorageSetDef> listStorageSets() throws StorageServiceException {
        try {
            return storageManager.listStorageSets();
        } catch (ConfigurationHandlerException e) {
            throw new StorageServiceException(e);
        }
    }

    public StorageSetDef getStorageSetDefinition(String storageSetName) throws StorageServiceException {

        return storageManager.getStorageSetDefinition(storageSetName);

    }

/*
############################################################
 STORAGE





############################################################
 */

    public StorageDef getStorageDefinition(String storageName) throws StorageServiceException {
        try {
            return storageManager.getStorageDefinition(storageName);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    public void modifyStorageDescription(String storageDefName, String description) throws StorageServiceException {
        try {
            storageManager.modifyStorageDescription(storageDefName, description);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    public void modifyStorageTitle(String storageDefName, String title) throws StorageServiceException {
        try {
            storageManager.modifyStorageTitle(storageDefName, title);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    public void modifyStorageMode(String storageDefName, StorageMode storageMode) throws StorageServiceException {

        try {
            storageManager.modifyStorageMode(storageDefName, storageMode);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }

    }

    public void modifyStorageTransformation(String storageDefName, String transformation) throws StorageServiceException {
        try {
            storageManager.modifyStorageTransformation(storageDefName, transformation);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    public void modifyStorageProperty(String storageDefName, Property property) throws StorageServiceException {
        try {
            storageManager.modifyStorageProperty(storageDefName, property);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    public void removeStorageProperty(String storageDefName, String propertyName) throws StorageServiceException {
        try {
            storageManager.removeStorageProperty(storageDefName, propertyName);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    public void addStorageProperty(String storageDefName, Property property) throws StorageServiceException {
        try {
            storageManager.addStorageProperty(storageDefName, property);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    @Transactional
    public void createStorage(String storageSetName, StorageDef storageDef) throws StorageServiceException {

        try {
            storageManager.createStorage(storageSetName, storageDef);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    @Transactional
    public void recreateCompleteContentForStorage(String storageName, StorageProgressWalker storageProgressWalker) throws StorageServiceException {

        try {
            contentManager.recreateCompleteContentForStorage(storageName, storageProgressWalker);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }

    }

    @Transactional
    public void removeStorage(String storageName) throws StorageServiceException {

        try {
            storageManager.removeStorage(storageName, true);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }

    }

    /**
     * This method just removes storage from definitions
     * Nothing is droped or deleted
     *
     * @param storageName
     * @throws StorageServiceException
     */
    @Transactional
    public void removeStorageDefinition(String storageName) throws StorageServiceException {

        try {
            storageManager.removeStorage(storageName, false);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    public List<StorageDef> listStorageDefinitionsForStorageSetDefinition(String storageSetName) throws StorageServiceException {
        try {
            return storageManager.listStorageDefsForStorageSetDef(storageSetName);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    public List<String> listStorageNames() throws StorageServiceException {
        try {
            return storageManager.listStorageNames();
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    public List<StorageDef> listStorages() throws StorageServiceException {

        try {
            return storageManager.listStorages();
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    /*
    ############################################################
     API KEYS





    ############################################################
     */

    public ApiKey createApiKey(String storageName, String name, String description, ApiMode apiMode) throws StorageServiceException {

        ApiKey resultApiKey = null;

        try {
            resultApiKey = storageManager.createAndWriteNewApiKeyForStorageId(storageName, name, description, apiMode);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }

        return resultApiKey;
    }

    public void removeApiKey(String storageName, ApiKey apiKey) throws StorageServiceException {

        try {
            storageManager.removeApiKey(storageName, apiKey);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    public void modifyApiKey(String storageName, ApiKey apiKey) throws StorageServiceException {

        try {
            storageManager.modifyApiKey(storageName, apiKey);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    public ApiKey verifyApiKey(String storageName, String apiKeyHash, ApiMode apiMode) throws StorageServiceException {

        try {
            return storageManager.verifyApiKey(storageName, apiKeyHash, apiMode);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }

    }

    public ApiKey getApiKey(String storageName, String apiKeyHash) throws StorageServiceException {

        throw new StorageServiceException("Method getApiKey not yet implemented.");

    }

    /*
    ############################################################
     CONTENT POOLS MANIPULATION





    ############################################################
     */
    public void createContentPool(String storageName, ContentPoolDef contentPoolDef) throws StorageServiceException {
        try {
            storageManager.createContentPool(storageName, contentPoolDef);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }

    }

    public void modifyContentPool(String storageName, ContentPoolDef contentPoolDef) throws StorageServiceException {
        try {
            storageManager.modifyContentPool(storageName, contentPoolDef);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    public void removeContentPool(String storageName, ContentPoolDef contentPoolDef) throws StorageServiceException {
        try {
            storageManager.removeContentPool(storageName, contentPoolDef);
        } catch (StorageManagerException e) {
            throw new StorageServiceException(e);
        }
    }

    /*
############################################################
 UTILS





############################################################
*/

    public List<String> listAvailableTransformations() {

        return storageManager.listAvailableTransformations();

    }

    public List<String> listAvailablePoolImplmentations() {

        return storageManager.listAvailablePoolImplementations();

    }

}
